import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit
from qt.online_client import OnlineWindow
from qt.offline_client import OfflineWindow
from module.sign_in import account_check
from module.log import *

class SignInUI(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 613)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # 纵向布局
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(230, 110, 311, 251))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")

        # 账号和密码输入框
        self.widget = QtWidgets.QWidget(self.verticalLayoutWidget)
        self.widget.setObjectName("widget")
        self.verticalLayout.addWidget(self.widget)

        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setContentsMargins(50, -1, 50, -1)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_account = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_account.setObjectName("label")
        self.horizontalLayout.addWidget(self.label_account)
        self.lineEdit_account = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_account.setObjectName("lineEdit_account")
        self.horizontalLayout.addWidget(self.lineEdit_account)
        self.verticalLayout.addLayout(self.horizontalLayout)

        self.widget_2 = QtWidgets.QWidget(self.verticalLayoutWidget)
        self.widget_2.setObjectName("widget_2")
        self.verticalLayout.addWidget(self.widget_2)

        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setContentsMargins(50, -1, 50, -1)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_password = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_password.setObjectName("label_password")
        self.horizontalLayout_2.addWidget(self.label_password)
        self.lineEdit_password = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_password.setObjectName("lineEdit_password")
        self.lineEdit_password.setEchoMode(QLineEdit.Password)
        self.horizontalLayout_2.addWidget(self.lineEdit_password)
        self.verticalLayout.addLayout(self.horizontalLayout_2)

        self.widget_3 = QtWidgets.QWidget(self.verticalLayoutWidget)
        self.widget_3.setObjectName("widget_3")
        self.verticalLayout.addWidget(self.widget_3)

        # 按钮
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.offline_pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.offline_pushButton.sizePolicy().hasHeightForWidth())
        self.offline_pushButton.setSizePolicy(sizePolicy)
        self.offline_pushButton.setObjectName("offline_pushButton")
        self.horizontalLayout_3.addWidget(self.offline_pushButton)

        self.online_pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.online_pushButton.sizePolicy().hasHeightForWidth())
        self.online_pushButton.setSizePolicy(sizePolicy)
        self.online_pushButton.setObjectName("online_pushButton")
        self.horizontalLayout_3.addWidget(self.online_pushButton)

        self.verticalLayout.addLayout(self.horizontalLayout_3)

        self.widget_4 = QtWidgets.QWidget(self.verticalLayoutWidget)
        self.widget_4.setObjectName("widget_4")
        self.verticalLayout.addWidget(self.widget_4)

        # 调整布局的比例
        self.verticalLayout.setStretch(0, 3)
        self.verticalLayout.setStretch(1, 1)
        self.verticalLayout.setStretch(2, 3)
        self.verticalLayout.setStretch(3, 1)
        self.verticalLayout.setStretch(4, 3)
        self.verticalLayout.setStretch(6, 3)

        # 设置主窗口的中央小部件
        MainWindow.setCentralWidget(self.centralwidget)

        # 菜单栏和状态栏
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.online_window =  None
        self.offline_window = None

        # 调用翻译函数
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "登录界面"))
        self.label_account.setText(_translate("MainWindow", "账号"))
        self.label_password.setText(_translate("MainWindow", "密码"))
        self.offline_pushButton.setText(_translate("MainWindow", "离线登陆"))
        self.online_pushButton.setText(_translate("MainWindow", "在线登陆"))


class SignInMainWindow(QMainWindow, SignInUI):
    def __init__(self, parent=None):
        super(SignInMainWindow, self).__init__(parent)
        self.db_path = os.path.join(os.getcwd(), "test.db")
        self.setupUi(self)

        self.offline_pushButton.clicked.connect(self.slot_offline_login)

        self.online_pushButton.clicked.connect(self.slot_online_login)

    def check_login(self):
        account = self.lineEdit_account.text()
        pw = self.lineEdit_password.text()

        if account_check(account=account, password=pw, db_path=self.db_path):
            return True

        "这里记得改成False"
        return True

    def slot_online_login(self):
        if self.check_login():
            self.online_window = OnlineWindow()
            self.online_window.logout_signal.connect(self.back_to_login)
            self.online_window.show()
            self.hide()
        else:
            """
            其实应该再弹个弹窗，懒得做了，直接打印一下得了
            """
            print("账户不存在。")


    def slot_offline_login(self):
        if self.check_login():
            self.offline_window = OfflineWindow(db_path=self.db_path)
            self.offline_window.logout_signal.connect(self.back_to_login)
            self.offline_window.show()
            self.hide()
        else:
            print("账户不存在。")


    def back_to_login(self):
        self.show()
        if self.online_window:
            self.online_window.close()
            self.online_window = None
        if self.offline_window:
            self.offline_window.close()
            self.offline_window = None

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = SignInMainWindow()
    main.show()
    sys.exit(app.exec_())
